package com.mask.share.life.jk.hander.handler_result;

import com.mask.share.life.jk.ExtInfo;
import com.mask.share.utils.ImageOpenCVUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Slf4j
public class MergeHandlerResult implements ImageHandlerResult {
    /**
     * 结果输出的文件夹
     */
    private String resultDir;

    public MergeHandlerResult(String resultDir) {
        this.resultDir = resultDir;
    }

    @Override
    public void handle(List<ExtInfo> extInfos) {
        log.info("结果:{}", extInfos);
        List<ExtInfo> list = extInfos.stream()
                .filter(Objects::nonNull)
                .filter(extInfo -> Objects.nonNull(extInfo.getMatchFlag()) && extInfo.getMatchFlag())
                .toList();

        if (CollectionUtils.isNotEmpty(list)) {
            Optional<ExtInfo> first = list.stream()
                    .sorted((e1, e2) -> -1 * Double.compare(e1.getSimilarity(), e2.getSimilarity()))
                    .findFirst();
            first.ifPresent(extInfo -> {
                ImageOpenCVUtils.saveMat(extInfo.getOrigImage(), resultDir + "\\" + extInfo.getSimilarity() + "_" + System.nanoTime() + ".jpg");
            });

        }
    }
}
